/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ventasautomotrices;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 *
 * @author Chapas Esta Clase hará las conexiones directas a la base de datos
 *
 */
public class AdminConexion {

    private Connection conn;

    public AdminConexion() {
        conn = null;
    }

    public boolean prueba() {
        System.out.println("Prueba de conexión MySQL .");
        conn = null;
        String url = "jdbc:mysql://localhost:3306/";
        String dbName = "AutoVentas";
        String driver = "com.mysql.jdbc.Driver";
        String userName = "ventasaut";
        String password = "pass123";
        try {
            Class.forName(driver).newInstance();
            conn = DriverManager.getConnection(url + dbName, userName, password);
            System.out.println("Connected to the database");
            conn.close();
            System.out.println("Disconnected from database");
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public boolean update(String sentence) {

        Connection conn = null;
        String url = "jdbc:mysql://localhost:3306/";
        String dbName = "AutoVentas";
        String driver = "com.mysql.jdbc.Driver";
        String userName = "ventasaut";
        String password = "pass123";
        try {
            Class.forName(driver).newInstance();
            conn = DriverManager.getConnection(url + dbName, userName, password);
            System.out.println("Connected to the database");

            Statement st = conn.createStatement();

            st.executeUpdate(sentence);

            conn.close();
            System.out.println("Update successful");
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

    }

    public boolean Query(String sentence) {

        Connection conn = null;
        String url = "jdbc:mysql://localhost:3306/";
        String dbName = "AutoVentas";
        String driver = "com.mysql.jdbc.Driver";
        String userName = "ventasaut";
        String password = "pass123";
        try {
            Class.forName(driver).newInstance();
            conn = DriverManager.getConnection(url + dbName, userName, password);
            System.out.println("Connected to the database");

            Statement st = conn.createStatement();

            st.executeQuery(sentence);
            conn.close();
            System.out.println("Query Successful");
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public String select(String sentence) {

        Connection conn = null;
        String url = "jdbc:mysql://localhost:3306/";
        String dbName = "AutoVentas";
        String driver = "com.mysql.jdbc.Driver";
        String userName = "ventasaut";
        String password = "pass123";
        try {
            Class.forName(driver).newInstance();
            conn = DriverManager.getConnection(url + dbName, userName, password);
            System.out.println("Connected to the database");

            Statement st = conn.createStatement();

            ResultSet rs = st.executeQuery(sentence);

            String str = "";
            while (rs.next()) {
                //marca modelo linea tipo color estado placa
                str += "Marca: " + rs.getString(1)
                        + " Modelo: " + rs.getString(2)
                        + " Linea: " + rs.getString(3)
                        + " Tipo: " + rs.getString(4)
                        + " Color: " + rs.getString(5)
                        + " Estado: " + rs.getString(6)
                        + " Placa: " + rs.getString(7)
                        + "\n";
            }

            conn.close();

            System.out.println("Query Successfull");
            return str;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public Connection abrirConexionBDD() throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException {
        conn = null;
        String url = "jdbc:mysql://localhost:3306/";
        String dbName = "AutoVentas";
        String driver = "com.mysql.jdbc.Driver";
        String userName = "ventasaut";
        String password = "pass123";
        Class.forName(driver).newInstance();
        conn = DriverManager.getConnection(url + dbName, userName, password);
        return conn;
    }

    public int cerrarConexionBDD() throws SQLException {
        conn.close();
        return 0;
    }

    public int login(String idUsuario, String password) throws SQLException {
        Statement st = conn.createStatement();
        String query = "SELECT idUsuarios FROM Usuarios WHERE idUsuarios='" + idUsuario + "' AND Password=md5('" + password + "')";
        ResultSet rs = st.executeQuery(query);
        int c = 0;
        while (rs.next()) {//Existe por lo menos un usuarios
            c++;
        }
        if (c == 1) {//Solo hay un usuario
            return 0;
        } else {
            return -1;//Datos incorrectos
        }
    }

    public int insert(PreparedStatement ps) throws SQLException {
        //System.out.println();
		//System.out.println( "++++++******" + ps.toString() + "+++++********");
        //System.out.println();
		return ps.executeUpdate();
    }

    public ResultSet consulta(String query) throws SQLException {
        ResultSet rs;
        Statement st = conn.createStatement();
        //System.out.println("Ejecutar: " + query);
        rs = st.executeQuery( query );

        return rs;
    }
    
    public PreparedStatement prepararConsulta(String query ) throws SQLException{
        PreparedStatement ret = conn.prepareStatement(query);
        return ret;
    }
}
